package cs

import (
	"github.com/spf13/cast"
	"learn/pkg/hepler"
	"strings"
)

type BaseService struct {
	file          string // 只展示字段 多字段 , 拼接
	addStatusName []statusName
}

// 添加 status name
type statusName struct {
	addName      string
	withFileName string
	fileNameArr  map[string]string
}

func (obj *BaseService) HasFile(file string) *BaseService {
	obj.file = file
	return obj
}

func (obj *BaseService) AddStatusName(addFileName, withFilName string, filaNameArr map[string]string) *BaseService {
	obj.addStatusName = append(obj.addStatusName, statusName{
		addName:      addFileName,
		withFileName: withFilName,
		fileNameArr:  filaNameArr,
	})
	return obj
}

func (obj *BaseService) Deal(input map[string]interface{}) map[string]interface{} {

	newMap := make(map[string]interface{})

	fileArr := []string{}

	if hepler.Empty(obj.file) == false {
		if strings.Contains(obj.file, ",") {
			fileArr = strings.Split(obj.file, ",")
		} else {
			fileArr = append(fileArr, obj.file)
		}
	}

	for i, v := range input {
		if hepler.Empty(obj.file) == false {
			if _, ok := hepler.Find(fileArr, i); ok == true {
				newMap[i] = v
			}
		} else {
			newMap[i] = v
		}
	}

	if hepler.Empty(obj.addStatusName) == false {
		for _, v := range obj.addStatusName {
			if fv, ok := input[v.withFileName]; ok != false {
				if fv1, ok1 := v.fileNameArr[cast.ToString(fv)]; ok1 != false {
					newMap[v.addName] = fv1
				}
			}
		}
	}

	return newMap
}

func (obj *BaseService) DealMap(input []map[string]interface{}) []map[string]interface{} {

	newMap := make([]map[string]interface{}, 0)

	for _, v := range input {
		newMap = append(newMap, obj.Deal(v))
	}

	return newMap
}
